package ch.mustaffah.aoide.util;

import java.util.ArrayList;

import ch.mustaffah.aoide.gui.Instruments;
import ch.mustaffah.aoide.gui.TimeMode;
import ch.mustaffah.aoide.model.Flat;
import ch.mustaffah.aoide.model.Score;
import ch.mustaffah.aoide.model.ScoreContainer;
import ch.mustaffah.aoide.model.Sharp;

public class Translator {

	public static String translateToJFugueString(ArrayList<String> scores, TimeMode time, Instruments instrument) {
		String result = "";
		for (String score : scores) {
			result = result + " " + score+time;
		}
		return  "I["+instrument +"]"+result;

	}

	public static String translateToRandomJFugueString(
			ArrayList<String> scores, ArrayList<TimeMode> timeModePattern, Instruments instrument) {
		String result = "";
		if (scores != null && scores.size() > 0) {
			for (int i = 0; i < timeModePattern.size(); i++) {
				int index = (int) (Math.random() * scores.size());
				result = result + " " + scores.get(index) + timeModePattern.get(i) +" ";
			}
		}

		return "I["+instrument +"]"+result;

	}

	public static ArrayList<String> getSelectedScores(
			ScoreContainer scoresContainer, Enum scaleContext) {
		Object[] scoresNames;
		Score[] scores = scoresContainer.getScorePattern();
		if (scaleContext instanceof Sharp) {
			scoresNames = Sharp.values();
		} else {
			scoresNames = Flat.values();
		}
		ArrayList<String> selected = new ArrayList<String>();
		for (int i = 0; i < scores.length; i++) {
			if (scores[i].isEnabled()) {
				String score = scoresNames[scores[i].getPosition()] + ""
						+ scoresContainer.getOctave();
				selected.add(score);
			}
		}
		return selected;
	}
	
	
	public static boolean [][] createSelectionPattern(){
		boolean [][] result = new boolean [12][8];
		for(int i=0;i<12;i++){
			for(int j=0; j<8;j++){
				int boolint=(int)(Math.random()*2);
				if(boolint==0){
					result[i][j]=false;	
				}else{
					result[i][j]=true;					
				}
			}
		}
		return result;
	}
	
	public static ArrayList<TimeMode> getRandomRhythmPattern(int lengthInThirtyTwoS){
		ArrayList<TimeMode> result=new ArrayList<TimeMode>();
		int countdown=lengthInThirtyTwoS;
		int timeModeCount=TimeMode.values().length;
		while(countdown>0){
			int index=(int)(Math.random()*timeModeCount);
			TimeMode mode=TimeMode.values()[index];
			if(mode==TimeMode.w && countdown > 32){
				result.add(mode);
				countdown=countdown-32;
			}else if(mode==TimeMode.h && countdown >= 16){
				result.add(mode);
				countdown=countdown-16;
			}else if(mode==TimeMode.q && countdown >= 8){
				result.add(mode);
				countdown=countdown-8;
			}else if(mode==TimeMode.i && countdown >= 4){
				result.add(mode);
				countdown=countdown-4;
			}else if(mode==TimeMode.s && countdown >= 2){
				result.add(mode);
				countdown=countdown-2;
			}else if(mode==TimeMode.t && countdown >= 1){
				result.add(mode);
				countdown=countdown-1;
			}
		}
		//mixit
		ArrayList<TimeMode> endresult=new ArrayList<TimeMode>();
		while(result.size()>0){
			int index=(int)(Math.random()*result.size());
			endresult.add(result.remove(index));
		}
		return endresult;
	}

	public static ArrayList<TimeMode> getRhythmPattern(TimeMode timeMode) {
		ArrayList<TimeMode> endresult=new ArrayList<TimeMode>();
		for(int i=0;i<4;i++){
			endresult.add(timeMode);
		}
		return endresult;
	}

}
